package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Blog;
import com.itheima.pojo.Tag;
import com.itheima.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/tag")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class WebTagController {

    @Autowired
    private TagService tagService;

    //通过标签Uid获取文章
    @GetMapping("/getArticleByTagUid")
    public Result getArticleByTagUid(@RequestParam("uid")String uid, @RequestParam(name = "currentPage",required = false) Integer currentPage, @RequestParam(name = "pageSize",required = false) Integer pageSize){
        PageResult pageResult= null;
        try {
            Page<Blog> blogPage =  tagService.getArticleByTagUid(uid,currentPage,pageSize);
            pageResult = new PageResult(blogPage.getTotal(),blogPage.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "查询数据异常");
        }

        return new Result("success", pageResult);
    }


    //获取标签的信息
    @GetMapping("/getTagList")
    public  Result getTagList(@RequestParam(name = "currentPage",required = false) Integer currentPage, @RequestParam(name = "pageSize",required = false) Integer pageSize){

        Page<Tag> blogPage = tagService.getTagList(currentPage,pageSize);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setTotal((int) blogPage.getTotal());
        result.setRecords(blogPage.getResult());
        return new Result("success",result) ;

    }
}
